// zh_jdgjb/pages/index/index_c.js
var app = getApp();
var util = require("../../utils/util.js");
var base = require('./../../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 城市名
    cityName:'',
    selectList: [],
    hotCity: [],
    strategy: [],
    adImg: '',
    day1: wx.getStorageSync('day1'),
    day2: wx.getStorageSync('day2'),
    tags:[
      {
        name:'北京',
        id:0
      }, {
        name: '上海',
        id: 1
      }, {
        name: '成都',
        id: 2
      },
      {
        name: '重庆',
        id: 3
      }, {
        name: '杭州',
        id: 4
      },
      {
        name: '西安',
        id: 5
      },
      {
        name: '南京',
        id: 6
      },
      {
        name: '武汉',
        id: 7
      },
      {
        name: '厦门',
        id: 8
      },
      {
        name: '全部',
        id: 9
      },  
    ]

  },
  bind_user_info: function (e) {
    var n = this;
    app.getUserInfo(function (e) {
      console.log(e);
      if (e.name == '' && e.img == '') console.log("这是没有检测到授权"), n.setData({
        users: true,
        level_name: "初始会员",
        userInfo: [],
        score: 0
      });
      else {
        if (n.setData({
          users: true
        }), 0 == e.level_id) var t = "初始会员";
        else t = e.level_name;
        n.setData({
          level_name: t,
          score: e.score,
          userInfo: e
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options.cityName);
    this.setData({
      cityName: options.cityName != "" && options.cityName != null? options.cityName:''
    })
    app.getUserInfo(function (e) {
      that.bind_user_info();
    });
    new Promise(function (resolve, reject){
      app.util.request({
        url: "entry/wxapp/attachurl",
        cachetime: "0",
        success: function (t) {
          wx.setStorageSync("url", t.data);
          resolve(1);
        },
        fail(){
          reject(1);
        }
      });
    }).then(function () {
      that.getDatas();
    }, function (err) {
      that.getDatas();
    })
  },
  getDatas () {
    var that= this;
    app.getUserInfo(function (e) {
      console.log(e), that.setData({
        userInfo: e
      });
    });
    that.setData({
      url: wx.getStorageSync("url")
    });
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res);
        wx.setStorageSync('latitude', res.latitude);
        wx.setStorageSync('longitude', res.longitude);
      }
    })
    // 头部样式
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    });
    // 精品推荐
    wx.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=SelectHotel&m=zh_jdgjb",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        that.setData({
          selectList: res.data.data.list
        });
      }
    });

    // 热门城市
    wx.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=HotCity&m=zh_jdgjb",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        that.setData({
          hotCity: res.data.data.list
        })
      }
    });
    // 精彩生活广告位
    app.util.request({
      url: "entry/wxapp/getad",
      cachetime: "0",
      data: {
        type: 4
      },
      success: function (t) {
        console.log(t.data[0].logo);
        that.setData({
          adImg: t.data[0].logo
        })
      }
    });
    // 攻略
    wx.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=StrategyCategory&m=zh_jdgjb",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        that.setData({
          strategy: res.data.data.list
        })
      }
    })
  },
  content: function(){
    // 查找酒店
    var e = this.data.platform;
    wx.navigateTo({
      url: "../hotel_list/hotel_list?nearby=0" + "&keywords=" + this.data.cityName
    });
  },
  data_time: function () {
    wx.navigateTo({
      url: './../date_time/index',
    })
  },
  strategyTo: function (e) {
    // 攻略跳转
    wx.navigateTo({
      url: './../article/art_list?title=' + e.currentTarget.dataset.title + '&id=' + e.currentTarget.dataset.id,
    })
  },
  search:function(){
    // 搜索附近酒店
    wx.navigateTo({
      url: "search?whereFrom=0",
      success: function (t) { },
      fail: function (t) { },
      complete: function (t) { }
    });
  },
  wode: function () {
    wx.reLaunch({
      url: "../logs/logs"
    });
  },
  detail: function(e) {
    // 酒店详情
    console.log(e.currentTarget.dataset.id);
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "./../hotel_list/hotel_info?hotel_id=" + id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  tagChoose: function (e) {
    var that = this;
    console.log(e);
    // wx.navigateTo({
    //   url: 'search?address=' + e.currentTarget.dataset.address,
    // })
    if (e.currentTarget.dataset.address == '全部'){
      e.currentTarget.dataset.address = '';
    }
    wx.navigateTo({
      url: "../hotel_list/hotel_list?nearby=0" + "&keywords=" + e.currentTarget.dataset.address
    });
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.util.request({
      url: "entry/wxapp/attachurl",
      cachetime: "0",
      success: function (t) {
        console.log(t), wx.setStorageSync("url", t.data);
      }
    });
    this.setData({
      day1: wx.getStorageSync('day1'),
      day2: wx.getStorageSync('day2')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})